package cn.buk.tms.util;

import cn.buk.common.util.DecimalUtil;
import cn.buk.tms.dto.request.HotelOrderRequestDto;
import cn.buk.tms.entity.misc.Customer;
import cn.buk.tms.entity.misc.PaymentMethod;
import cn.buk.tms.entity.misc.Supplier;
import cn.buk.tms.entity.order.hotel.HotelOrder;
import cn.buk.tms.entity.order.hotel.HotelOrderPassenger;
import cn.buk.tms.entity.order.hotel.HotelOrderPrice;
import cn.buk.tms.hotel.dto.HotelOrderPassengerDto;
import cn.buk.tms.hotel.dto.HotelOrderPriceDto;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;

import static cn.buk.tms.common.constant.TmsOrderConstant.PAY_TYPE_BILL;

public class HotelOrderUtil {

  public static HotelOrder convertToHotelOrder(HotelOrderRequestDto dto) {
    dto.processPassengerInfo();

    HotelOrder o = new HotelOrder();
    BeanUtils.copyProperties(dto, o, "passengers", "prices");

    if (dto.getPayType() == 0) {
      // 默认为记账
      o.setPayType(PAY_TYPE_BILL);
    }

    o.setPassengers(null);
    String psgNames = null;
    for(HotelOrderPassengerDto psgDto: dto.getPassengers()) {

      HotelOrderPassenger psg = new HotelOrderPassenger();
      psg.setOrderInfo(o);
      psg.setEnterpriseId(o.getEnterpriseId());

      BeanUtils.copyProperties(psgDto, psg);

      o.getPassengers().add(psg);

      if (psgNames == null) {
        psgNames = psgDto.getName();
      } else {
        psgNames += "," + psgDto.getName();
      }
    }

    o.setPsgCount(o.getPassengers().size());

    o.setCustomer(null);
    if (dto.getCustomerId() > 0) {
      Customer c = new Customer();
      c.setId(dto.getCustomerId());

      o.setCustomer(c);
      //企业客户
      o.setCustomerType(Customer.ENT_CORPORATION);
    } else {
      // 散客
      o.setCustomerType(Customer.ENT_INDIVIDUAL);
    }

    if (dto.getSupplierId() > 0) {
      Supplier supplier = new Supplier();
      supplier.setId(dto.getSupplierId());

      o.setSupplier(supplier);
    }

    if (dto.getPaymentMethodId() > 0) {
      PaymentMethod pm = new PaymentMethod();
      pm.setId(dto.getPaymentMethodId());

      o.setPaymentMethod(pm);
    }

    o.setPrices(null);
    for (HotelOrderPriceDto priceDto: dto.getPrices()) {
      HotelOrderPrice price = new HotelOrderPrice();
      price.setOrderInfo(o);
      price.setEnterpriseId(o.getEnterpriseId());

      BeanUtils.copyProperties(priceDto, price);

      o.getPrices().add(price);
    }

    return o;
  }

  public static HotelOrderRequestDto convertToHotelOrderRequestDto(HotelOrder o) {
    HotelOrderRequestDto dto = new HotelOrderRequestDto();
    BeanUtils.copyProperties(o, dto);

    dto.setPassengers(new ArrayList<>());
    String psgNames = null;

    for (HotelOrderPassenger psg : o.getPassengers()) {

      HotelOrderPassengerDto psgDto = new HotelOrderPassengerDto();

      BeanUtils.copyProperties(psg, psgDto);

      dto.getPassengers().add(psgDto);

      if (psgNames == null) {
        psgNames = psg.getName();
      } else {
        psgNames += "," + psg.getName();
      }
    }

    if (psgNames != null) {
      dto.setPsgCount(dto.getPassengers().size());
      dto.setPassengerName(psgNames);
    }

    if (o.getCustomer() == null) {
      dto.setCustomerId(0);
      dto.setCustomerName("散客");
    } else {
      dto.setCustomerId(o.getCustomerId());
      dto.setCustomerName(o.getCustomer().getVipName());
      dto.setCustomerCode(o.getCustomer().getCustomerCode());
    }

    if (o.getSupplier() != null) {
      dto.setSupplierId(o.getSupplier().getId());
      dto.setSupplierName(o.getSupplier().getName());
    }

    if (o.getPaymentMethod() != null) {
      dto.setPaymentMethodId(o.getPaymentMethod().getId());
      dto.setPaymentMethodName(o.getPaymentMethod().getName());
    }

    dto.setProfit(DecimalUtil.keepScale2(dto.getProfit()));
    dto.setTotalCost(DecimalUtil.keepScale2(dto.getTotalCost()));

    for(HotelOrderPrice price: o.getPrices()) {
      HotelOrderPriceDto priceDto = new HotelOrderPriceDto();
      BeanUtils.copyProperties(price, priceDto);

      dto.getPrices().add(priceDto);
    }

    return dto;
  }

}
